//
//  YHPickerViewController.m
//  TallyBook
//
//  Created by 林宁宁 on 16/9/11.
//  Copyright © 2016年 林宁宁. All rights reserved.
//

#import "YHPickerViewController.h"
#import "NSDate+YH.h"
#import "UIColor+YHStyle.h"
#import "UIFont+YH.h"
#import "UIButton+YH.h"
#import <YYCategories/NSDate+YYAdd.h>
#import "YHDefines.h"
#import "NSString+YH.h"
#import "UIView+YH.h"
#import "YHDeviceInfo.h"
#import "YHConstant+Null.h"


//NSInteger YHDatePickerMinute30Index(NSInteger )

@interface YHDatePickerItem : NSObject

//@property (nonatomic, assign) NSInteger currentMonthDay;
@property (nonatomic, assign) NSInteger currentYear;
@property (nonatomic, assign) NSInteger currentMonth;
@property (nonatomic, assign) NSInteger currentDay;
@property (nonatomic, assign) NSInteger currentHour;
@property (nonatomic, assign) NSInteger currentMinute;
@property (nonatomic, assign) NSInteger currentSecond;

@property (nonatomic, assign) NSInteger yearMin;
@property (nonatomic, assign) NSInteger yearMax;

@property (nonatomic, assign) NSInteger year;
@property (nonatomic, assign) NSInteger month;
@property (nonatomic, assign) NSInteger day;
@property (nonatomic, assign) NSInteger hour;
@property (nonatomic, assign) NSInteger minute;
@property (nonatomic, assign) NSInteger second;

@property (nonatomic, copy) NSString * dateDescrption;

@end

@implementation YHDatePickerItem

@end

@interface YHPickerItem : NSObject

//类型
@property (copy, nonatomic)     NSString * pickerTitle;
@property (retain, nonatomic)   NSArray <NSString *>* pickerDataList;
@property (copy, nonatomic)     NSString * pickerSelectTitle;
//  获取标题
@property (copy, nonatomic) NSString *(^getPickerTitleBlock)(NSInteger index);
- (instancetype)initWithTitle:(NSString *)title andPickerList:(NSArray <NSString *>*)list andPickerSelectTitle:(NSString *)selectTitle;
@end

@implementation YHPickerItem

-(instancetype)initWithTitle:(NSString *)title andPickerList:(NSArray<NSString *> *)list andPickerSelectTitle:(NSString *)selectTitle{
    self = [super init];
    if(self){
        self.pickerDataList = [NSArray arrayWithArray:list];
        self.pickerTitle = title;
        self.pickerSelectTitle = selectTitle;
    }
    return self;
}
@end


@interface YHPickerConfig()
/** 选中的是什么 年月日时分*/
@property (retain, nonatomic) YHDatePickerItem * pickerDateSelect;

/** 这个里面的数据是 YHPickerItem 类型  自定义类型的时候*/
@property (retain, nonatomic) NSArray<YHPickerItem *> * dataList;

@end

@implementation YHPickerConfig

- (void)yh_commonInit{
    self.pickerDateSelect = [YHDatePickerItem new];
    self.pickerDateSelect.yearMin = 1900;
    self.pickerDateSelect.yearMax = [NSDate date].year + 100;
}


-(void)setDateOrigin:(NSDate *)dateOrigin{
    _dateOrigin = dateOrigin;
    if(!self.pickerDateSelect){
        return;
    }
    
    self.pickerDateSelect.year = dateOrigin.year;
    self.pickerDateSelect.month = dateOrigin.month;
    self.pickerDateSelect.day = dateOrigin.day;
    self.pickerDateSelect.hour = dateOrigin.hour;
    self.pickerDateSelect.minute = dateOrigin.minute;
    self.pickerDateSelect.second = dateOrigin.second;
    self.pickerDateSelect.dateDescrption = [dateOrigin yh_stringByFormate:nil];
    
//    self.pickerDateSelect.currentMonthDay = dateOrigin.yh_identifyToDay.integerValue;
    self.pickerDateSelect.currentYear = dateOrigin.year;
    self.pickerDateSelect.currentMonth = dateOrigin.month;
    self.pickerDateSelect.currentDay = dateOrigin.day;
    self.pickerDateSelect.currentHour = dateOrigin.hour;
    self.pickerDateSelect.currentMinute = dateOrigin.minute;
    self.pickerDateSelect.currentSecond = dateOrigin.second;
}


- (NSString *)currentDateString{
    NSMutableString * dateStr = [[NSMutableString alloc] init];
    
    [dateStr appendFormat:@"%04ld-",self.pickerDateSelect.year];
    [dateStr appendFormat:@"%02ld-",self.pickerDateSelect.month];
    [dateStr appendFormat:@"%02ld ",self.pickerDateSelect.day];
    
    [dateStr appendFormat:@"%02ld:",self.pickerDateSelect.hour];
    if(self.pickerMode & YHPickerDateType_Minute30){
        [dateStr appendFormat:@"%02ld:",(NSInteger)(self.pickerDateSelect.minute/30.0)*30];
    }else{
        [dateStr appendFormat:@"%02ld:",self.pickerDateSelect.minute];
    }
    if(self.pickerMode & YHPickerDateType_Second){
        [dateStr appendFormat:@"%02ld",self.pickerDateSelect.second];
    }else{
        [dateStr appendString:@"00"];
    }
    
    return dateStr;
}


@end


@interface YHPickerViewController()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (retain, nonatomic) UIPickerView * datePicker;

@property (retain, nonatomic) UIView * toolV;

@property (assign, nonatomic) float component_width;

@property (retain, nonatomic) UILabel * barLeftLabel;
@property (retain, nonatomic) UILabel * barCenterLabel;
@property (retain, nonatomic) UIButton * barBtnRight;

@property (retain, nonatomic) YHPickerConfig * config;

@end

@implementation YHPickerViewController

-(YHPickerConfig *)config{
    if(!_config){
        _config = [YHPickerConfig new];
    }
    return _config;
}

- (void)yh_setupUI{
    
    self.popFromBottom = YES;
    CGFloat contentHeight = 0;
    
    self.contentView.backgroundColor = [UIColor yh_background];
    {
        self.toolV = [UIView new];
        self.toolV.backgroundColor = [UIColor clearColor];
        
        self.barCenterLabel = [UILabel new];
        self.barCenterLabel.textAlignment = NSTextAlignmentCenter;
        self.barCenterLabel.textColor = [UIColor yh_h1];
        self.barCenterLabel.font = [UIFont yh_pfOfSize:14];
        [self.toolV addSubview:self.barCenterLabel];
        [self.barCenterLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.toolV);
        }];
        
        self.barLeftLabel = [UILabel new];
        self.barLeftLabel.textAlignment = NSTextAlignmentLeft;
        self.barLeftLabel.textColor = [UIColor yh_h1];
        self.barLeftLabel.font = [UIFont yh_pfOfSize:14];
        [self.toolV addSubview:self.barLeftLabel];
        [self.barLeftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.toolV).offset(20);
            make.centerY.equalTo(self.toolV);
        }];
        
        self.barBtnRight = [UIButton buttonWithType:UIButtonTypeCustom];
        self.barBtnRight.backgroundColor = [UIColor clearColor];
        self.barBtnRight.contentEdgeInsets = UIEdgeInsetsMake(5, 10, 5, 10);
        [self.barBtnRight setTitle:@"今天" forState:UIControlStateNormal];
        [self.barBtnRight setTitleColor:[UIColor yh_h1] forState:UIControlStateNormal];
        self.barBtnRight.titleLabel.font = [UIFont yh_pfOfSize:14];
        [self.barBtnRight addTarget:self action:@selector(pickerRightAction) forControlEvents:UIControlEventTouchUpInside];
        [self.toolV addSubview:self.barBtnRight];
        [self.barBtnRight mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.toolV).offset(-20);
            make.centerY.equalTo(self.toolV);
        }];
        
        [self.contentView addSubview:self.toolV];
        [self.toolV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView);
            make.top.equalTo(self.contentView);
            make.height.mas_equalTo(50);
        }];

        self.barBtnRight.hidden = YES;
        
        contentHeight = contentHeight + 50;
    }
    {
        self.datePicker = [[UIPickerView alloc] init];
        self.datePicker.delegate = self;
        self.datePicker.dataSource = self;
        self.datePicker.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.datePicker];
        
        [self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView);
            make.top.equalTo(self.toolV.mas_bottom);
            make.height.mas_equalTo(230);
        }];
        
        contentHeight = contentHeight + 230;
    }
    {
        UIButton * btnSubmit = [UIButton buttonWithType:UIButtonTypeCustom];
        btnSubmit.backgroundColor = [[UIColor yh_background] lighterColor:0.2];
        [btnSubmit setTitle:@"确定" forState:UIControlStateNormal];
        [btnSubmit setTitleColor:[UIColor yh_h1] forState:UIControlStateNormal];
        btnSubmit.titleLabel.font = [UIFont yh_pfOfSize:14];
        [btnSubmit addTarget:self action:@selector(pickerNumSubmit) forControlEvents:UIControlEventTouchUpInside];
        btnSubmit.layer.cornerRadius = 20;
        btnSubmit.layer.masksToBounds = YES;
        [self.contentView addSubview:btnSubmit];
        [btnSubmit mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-20);
            make.left.equalTo(self.contentView).offset(20);
            make.bottom.equalTo(self.contentView).offset(-10-[YHDeviceInfo height_home_indicator]);
            make.height.mas_equalTo(44);
        }];
        
        contentHeight = contentHeight + 10 + [YHDeviceInfo height_home_indicator] + 44 + 10;
    }
    
    contentHeight = contentHeight + 20;
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.bottom.equalTo(self).offset(20);
        make.height.mas_equalTo(contentHeight);
    }];
}

- (void)yh_setupData{
    
    NSDate * date = [NSDate date];
    if(self.config.dateOrigin){
        date = self.config.dateOrigin;
    }
    if(self.config.pickerMode & YHPickerDateType_Minute30){
        date = [date dateByAddingMinutes:30];
    }
    self.config.dateOrigin = date;
    
    CGFloat s_wdith = [YHDeviceInfo s_width];
    if(self.config.pickerMode == YHPickerDateType_Custom){
        self.component_width = (s_wdith-(self.config.dataList.count-1)*5)/self.config.dataList.count;
    }
    else{
        NSInteger count = [self typeShowComponentCount];
        self.component_width = s_wdith/count;
    }
    
    if(self.config.pickerMode != YHPickerDateType_Custom &&
       self.config.showTodayPicker){
        self.barBtnRight.hidden = NO;
    }
    if(self.config.barRightBtnConfig){
        self.barBtnRight.hidden = NO;
        self.config.barRightBtnConfig(self.barBtnRight);
    }
    
    self.barCenterLabel.text = NullShow(self.config.pickerHeadTitle);
    
    [self resetOriginAnimation];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    [self.contentView cornerTop:20];
}

- (void)pickerNumSubmit{
    if(self.config.pickerFinishBlock){
        if(self.config.pickerMode == YHPickerDateType_Custom){
            self.config.pickerFinishBlock(self.config.dataList);
        }
        else{
            self.config.pickerFinishBlock([self.config currentDateString]);
        }
    }
    [self popDismiss];
}

- (void)pickerRightAction{
    if(self.config.barRightBtnAction){
        self.config.barRightBtnAction();
        [self popDismiss];
    }else{
        if(self.config.pickerMode != YHPickerDateType_Custom){
            self.config.dateOrigin = [NSDate date];
            [self resetOriginAnimation];
        }
    }
}

#pragma mark - picker view的代理

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    if(self.config.pickerMode == YHPickerDateType_Custom){
        return self.config.dataList.count;
    }
    return [self typeShowComponentCount];
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if(self.config.pickerMode == YHPickerDateType_Custom){
        YHPickerItem * item = self.config.dataList[component];
        return item.pickerDataList.count;
    }
    YHPickerDateType type = [self typeShowAtComponent:component];
    switch (type) {
        case YHPickerDateType_MonthDayWeek:
            return 3*30;
            break;
        case YHPickerDateType_Year:
            //年
            return self.config.pickerDateSelect.yearMax - self.config.pickerDateSelect.yearMin;
            break;
        case YHPickerDateType_Month:
            //月
            return 12;
            break;
        case YHPickerDateType_Day:
            //日
            return [self getDayCountAtSelectMonth];
            break;
        case YHPickerDateType_Hour:
            //时
            return 24;
            break;
        case YHPickerDateType_Minute:
            //分
            return 60;
            break;
        case YHPickerDateType_Minute30: return 2;
        case YHPickerDateType_Second:
            //秒
            return 60;
            break;
        default:
            
            break;
    }
    return 0;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    if(self.config.pickerMode == YHPickerDateType_Custom){
        return self.component_width;
    }
    YHPickerDateType type = [self typeShowAtComponent:component];
    if(type == YHPickerDateType_MonthDayWeek){
        return self.component_width*1.2;
    }
    else if(type == YHPickerDateType_Year){
        return self.component_width;
    }
    else{
        return self.component_width*0.65;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 44;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    NSMutableString * titleShow = [NSMutableString new];
    float width_component = self.component_width;
    if(self.config.pickerMode == YHPickerDateType_Custom){
        YHPickerItem * item = self.config.dataList[component];
        [titleShow appendString:item.pickerDataList[row]];
    }
    else{
        YHPickerDateType type = [self typeShowAtComponent:component];
        switch (type) {
            case YHPickerDateType_MonthDayWeek:{
                //年
                if(row == 0){
                    [titleShow appendString:@"今天"];
                }else if(row == 1){
                    [titleShow appendString:@"明天"];
                }else{
                    NSDate * date = [self.config.dateOrigin dateByAddingDays:row];
                    [titleShow appendFormat:@"%@ ",[date yh_stringByFormate:@"MM月dd日"]];
                    [titleShow appendFormat:@"周%@", [date yh_dateWeekday]];
                }
            }
                break;
            case YHPickerDateType_Year:{
                //年
                [titleShow appendFormat:@"%04ld",(self.config.pickerDateSelect.yearMax - row)];
                if(self.config.showDateCN){
                    [titleShow appendString:@"年"];
                }
            }
                break;
            case YHPickerDateType_Month:{
                //月
                [titleShow appendFormat:@"%02ld",row+1];
                if(self.config.showDateCN){
                    [titleShow appendString:@"月"];
                }
                width_component = self.component_width * 0.65;
            }
                break;
            case YHPickerDateType_Day:{
                //日
                [titleShow appendFormat:@"%02ld",row+1];
                if(self.config.showDateCN){
                    [titleShow appendString:@"日"];
                }
                width_component = self.component_width * 0.65;
            }
                break;
            case YHPickerDateType_Hour:{
                //时
                [titleShow appendFormat:@"%02ld",row];
                if(self.config.showDateCN){
                    [titleShow appendString:@"时"];
                }
                width_component = self.component_width * 0.65;
            }
                break;
            case YHPickerDateType_Minute:{
                //分
                [titleShow appendFormat:@"%02ld",row];
                if(self.config.showDateCN){
                    [titleShow appendString:@"分"];
                }
                width_component = self.component_width * 0.65;
            }
                break;
            case YHPickerDateType_Minute30:{
                //分
                [titleShow appendFormat:@"%02ld",row*30];
                if(self.config.showDateCN){
                    [titleShow appendString:@"分"];
                }
                width_component = self.component_width * 0.65;
            }
                break;
            case YHPickerDateType_Second:
            {
                //秒
                [titleShow appendFormat:@"%02ld",row];
                if(self.config.showDateCN){
                    [titleShow appendString:@"秒"];
                }
                width_component = self.component_width * 0.65;
            }
                break;
            default:
                
                break;
        }
    }
    
    UILabel * labTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width_component, 44)];
    labTitle.text = titleShow;
    labTitle.textAlignment = NSTextAlignmentCenter;
    labTitle.textColor = [UIColor yh_h1];
    labTitle.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    labTitle.font = [UIFont yh_pflOfSize:17];
    labTitle.adjustsFontSizeToFitWidth = YES;
    return labTitle;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    if(self.config.pickerMode == YHPickerDateType_Custom){
        YHPickerItem * item = self.config.dataList[component];
        if(item.pickerDataList.count > 0){
            item.pickerSelectTitle = item.pickerDataList[row];
        }
    }else{
        YHPickerDateType type = [self typeShowAtComponent:component];
        switch (type) {
            case YHPickerDateType_MonthDayWeek:{
                NSDate * date = [self.config.dateOrigin dateByAddingDays:row];
                self.config.pickerDateSelect.year = date.year;
                self.config.pickerDateSelect.month = date.month;
                self.config.pickerDateSelect.day = date.day;
                [self reloadDayComponent];
            }
                break;
            case YHPickerDateType_Year:
                //年
                self.config.pickerDateSelect.year = self.config.pickerDateSelect.yearMax - row;
                [self reloadDayComponent];
                break;
            case YHPickerDateType_Month:
                //月
                self.config.pickerDateSelect.month = row + 1;
                [self reloadDayComponent];
                break;
            case YHPickerDateType_Day:
                self.config.pickerDateSelect.day = row + 1;
                break;
            case YHPickerDateType_Hour:
                //时
                self.config.pickerDateSelect.hour = row;
                break;
            case YHPickerDateType_Minute:
                //分
                self.config.pickerDateSelect.minute = row;
                break;
            case YHPickerDateType_Minute30:
                //分
                self.config.pickerDateSelect.minute = row*30;
                break;
            case YHPickerDateType_Second:
                //秒
                self.config.pickerDateSelect.second = row;
                break;
            default:
                
                break;
        }
        
        NSString * dateStr = [self.config currentDateString];
        self.config.pickerDateSelect.dateDescrption = dateStr;
        
        NSDate * selectDate = [dateStr yh_dateByFormate:nil];
        if(!selectDate){
            [self resetOriginAnimation];
        }else if(self.config.picker_able == YHPickerDateSelectAble_old){
            if([selectDate timeIntervalSinceNow] > 0){
                [self resetOriginAnimation];
            }
        }
        else if(self.config.picker_able == YHPickerDateSelectAble_future){
            if([selectDate timeIntervalSinceNow] < 0){
                [self resetOriginAnimation];
            }
        }
    }
    
    [self pickerSelectDidChange];
}


//一共有几块组件
- (NSInteger)typeShowComponentCount{
    NSInteger count = 0;
    if(self.config.pickerMode & YHPickerDateType_MonthDayWeek){
        count++;
    }
    if(self.config.pickerMode & YHPickerDateType_Year){
        count++;
    }
    if(self.config.pickerMode & YHPickerDateType_Month){
        count++;
    }
    if(self.config.pickerMode & YHPickerDateType_Day){
        count++;
    }
    if(self.config.pickerMode & YHPickerDateType_Hour){
        count++;
    }
    if(self.config.pickerMode & YHPickerDateType_Minute){
        count++;
    }
    if(self.config.pickerMode & YHPickerDateType_Minute30){
        count++;
    }
    if(self.config.pickerMode & YHPickerDateType_Second){
        count++;
    }
    return count;
}

//一共有几块组件
- (NSInteger)typeShowAtComponentIndex:(YHPickerDateType)type{
    if(self.config.pickerMode & type){
        NSInteger index = 0;
        if(self.config.pickerMode & YHPickerDateType_MonthDayWeek){
            if(type == YHPickerDateType_MonthDayWeek){
                return index;
            }
            index++;
        }
        if(self.config.pickerMode & YHPickerDateType_Year){
            if(type == YHPickerDateType_Year){
                return index;
            }
            index++;
        }
        if(self.config.pickerMode & YHPickerDateType_Month){
            if(type == YHPickerDateType_Month){
                return index;
            }
            index++;
        }
        if(self.config.pickerMode & YHPickerDateType_Day){
            if(type == YHPickerDateType_Day){
                return index;
            }
            index++;
        }
        if(self.config.pickerMode & YHPickerDateType_Hour){
            if(type == YHPickerDateType_Hour){
                return index;
            }
            index++;
        }
        if(self.config.pickerMode & YHPickerDateType_Minute){
            if(type == YHPickerDateType_Minute){
                return index;
            }
            index++;
        }
        if(self.config.pickerMode & YHPickerDateType_Minute30){
            if(type == YHPickerDateType_Minute30){
                return index;
            }
            index++;
        }
        if(self.config.pickerMode & YHPickerDateType_Second){
            if(type == YHPickerDateType_Second){
                return index;
            }
            index++;
        }
    }
    return -1;
}

//某块组件下是什么日期类型
- (YHPickerDateType)typeShowAtComponent:(NSInteger)component{
    NSInteger count = 0;
    if(self.config.pickerMode & YHPickerDateType_MonthDayWeek){
        if(component == count){
            return YHPickerDateType_MonthDayWeek;
        }
        count++;
    }
    if(self.config.pickerMode & YHPickerDateType_Year){
        if(component == count){
            return YHPickerDateType_Year;
        }
        count++;
    }
    if(self.config.pickerMode & YHPickerDateType_Month){
        if(component == count){
            return YHPickerDateType_Month;
        }
        count++;
    }
    if(self.config.pickerMode & YHPickerDateType_Day){
        if(component == count){
            return YHPickerDateType_Day;
        }
        count++;
    }
    if(self.config.pickerMode & YHPickerDateType_Hour){
        if(component == count){
            return YHPickerDateType_Hour;
        }
        count++;
    }
    if(self.config.pickerMode & YHPickerDateType_Minute){
        if(component == count){
            return YHPickerDateType_Minute;
        }
        count++;
    }
    if(self.config.pickerMode & YHPickerDateType_Minute30){
        if(component == count){
            return YHPickerDateType_Minute30;
        }
        count++;
    }
    if(self.config.pickerMode & YHPickerDateType_Second){
        if(component == count){
            return YHPickerDateType_Second;
        }
        count++;
    }
    return -1;
}

- (void)reloadDayComponent{
    NSInteger component = [self typeShowAtComponentIndex:YHPickerDateType_Day];
    if(component > -1){
        [self.datePicker reloadComponent:component];
        NSInteger dayCount = [self getDayCountAtSelectMonth];
        if(self.config.pickerDateSelect.day > dayCount){
            self.config.pickerDateSelect.day = dayCount;
        }
    }
}

- (void)resetOriginAnimation{
    if(self.config.pickerMode == YHPickerDateType_Custom){
        //设置当前选中的是哪个选项
        [self.config.dataList enumerateObjectsUsingBlock:^(YHPickerItem * obj, NSUInteger idx, BOOL *stop) {
            if(IsNonNull(obj.pickerSelectTitle)){
                if([obj.pickerDataList containsObject:obj.pickerSelectTitle]){
                    NSInteger index = [obj.pickerDataList indexOfObject:obj.pickerSelectTitle];
                    [self.datePicker selectRow:index inComponent:idx animated:YES];
                }
            }
        }];
        
        [self pickerSelectDidChange];
        return;
    }

    self.config.pickerDateSelect.dateDescrption = [self.config currentDateString];

//    self.config.pickerDateSelect.currentMonthDay = self.config.pickerDateSelect.currentMonthDay;
    self.config.pickerDateSelect.year = self.config.pickerDateSelect.currentYear;
    self.config.pickerDateSelect.month = self.config.pickerDateSelect.currentMonth;
    self.config.pickerDateSelect.day = self.config.pickerDateSelect.currentDay;
    self.config.pickerDateSelect.hour = self.config.pickerDateSelect.currentHour;
    self.config.pickerDateSelect.minute = self.config.pickerDateSelect.currentMinute;
    self.config.pickerDateSelect.second = self.config.pickerDateSelect.currentSecond;

    NSInteger offsetYear = self.config.pickerDateSelect.yearMax - self.config.pickerDateSelect.year;
    NSInteger offsetMonth = self.config.pickerDateSelect.currentMonth - 1;
    NSInteger offsetDay = self.config.pickerDateSelect.currentDay - 1;
    NSInteger offsetHour = self.config.pickerDateSelect.currentHour;
    NSInteger offsetMintue = self.config.pickerDateSelect.currentMinute;
    NSInteger offsetMintue30 = self.config.pickerDateSelect.currentMinute/30.0;
    NSInteger offsetSecond = self.config.pickerDateSelect.currentSecond;
    
    NSInteger componentCount = [self typeShowComponentCount];
    for(NSInteger component = 0; component < componentCount; component++){
        YHPickerDateType type = [self typeShowAtComponent:component];
        switch (type) {
            case YHPickerDateType_MonthDayWeek:
                //年
                [self.datePicker selectRow:0 inComponent:component animated:YES];
                break;
            case YHPickerDateType_Year:
                //年
                [self.datePicker selectRow:offsetYear inComponent:component animated:YES];
                break;
            case YHPickerDateType_Month:
                //月
                [self.datePicker selectRow:offsetMonth inComponent:component animated:YES];
                break;
            case YHPickerDateType_Day:
                [self.datePicker selectRow:offsetDay inComponent:component animated:YES];
                break;
            case YHPickerDateType_Hour:
                //时
                [self.datePicker selectRow:offsetHour inComponent:component animated:YES];
                break;
            case YHPickerDateType_Minute:
                //分
                [self.datePicker selectRow:offsetMintue inComponent:component animated:YES];
                break;
            case YHPickerDateType_Minute30:
                //分
                [self.datePicker selectRow:offsetMintue30 inComponent:component animated:YES];
                break;
            case YHPickerDateType_Second:
                //秒
                [self.datePicker selectRow:offsetSecond inComponent:component animated:YES];
                break;
            default:
                break;
        }
    }
    
    [self pickerSelectDidChange];
}

- (NSInteger)getDayCountAtSelectMonth{
    if(self.config.pickerDateSelect.month == 1 ||
       self.config.pickerDateSelect.month == 3 ||
       self.config.pickerDateSelect.month == 5 ||
       self.config.pickerDateSelect.month == 7 ||
       self.config.pickerDateSelect.month == 8 ||
       self.config.pickerDateSelect.month == 10 ||
       self.config.pickerDateSelect.month == 12){
        return 31;
    }
    else if (self.config.pickerDateSelect.month == 2){
        // 2月份，闰年29天、平年28天
        if ((self.config.pickerDateSelect.year % 4 == 0 && self.config.pickerDateSelect.year % 100 != 0) ||
            self.config.pickerDateSelect.year % 400 == 0){
            return 29;
        }
        else{
            return 28;
        }
    }
    else{
        return 30;
    }
}

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component{
    if(component >= [self numberOfComponentsInPickerView:self.datePicker]){
        return;
    }
    if(row >= [self pickerView:self.datePicker numberOfRowsInComponent:component]){
        return;
    }
    [self.datePicker selectRow:row inComponent:component animated:YES];
}

#pragma mark -

- (void)pickerSelectDidChange{
    if(self.config.pickerMode == YHPickerDateType_Custom){
        if(self.config.pickerDidChangeBlock){
            NSMutableString * title = [NSMutableString new];
            [self.config.dataList enumerateObjectsUsingBlock:^(YHPickerItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if(IsNull(obj.pickerSelectTitle)){
                    obj.pickerSelectTitle = obj.pickerDataList.firstObject;
                }
                [title appendString:NullShow(obj.pickerSelectTitle)];
            }];
            NSString * hint = self.config.pickerDidChangeBlock(title);
            self.barLeftLabel.text = NullShow(hint);
        }
    }else{
        if(self.config.pickerDidChangeBlock){
            NSString * dateStr = [self.config currentDateString];
            NSDate * date = [dateStr yh_dateByFormate:nil];
            if(self.config.dateFormat){
                dateStr = [date yh_stringByFormate:self.config.dateFormat];
            }
            NSString * hint = self.config.pickerDidChangeBlock(dateStr);
            self.barLeftLabel.text = NullShow(hint);
        }
    }
}

#pragma mark -

+ (void)showPickerDateType:(YHPickerDateType)dateType
                originDate:(NSDate * _Nullable)originDate
                    format:(NSString * _Nullable)format
          didiChangePicker:(nullable NSString * _Nonnull (^)(NSString * _Nonnull))hintBlock
                    finish:(void (^ _Nullable)(NSDate * _Nonnull))finish{
    [YHPickerViewController showPickerDateType:dateType originDate:originDate format:format didiChangePicker:hintBlock finish:finish config:nil];
}
/// 选择时间
+ (void)showPickerDateType:(YHPickerDateType)dateType
                originDate:(NSDate * _Nullable)originDate
                    format:(NSString * _Nullable)format
          didiChangePicker:(nullable NSString * _Nonnull (^)(NSString * _Nonnull))hintBlock
                    finish:(void (^ _Nullable)(NSDate * _Nonnull))finish
                    config:(void (^ _Nullable)(YHPickerConfig * _Nonnull))config{
    [YHPickerViewController showPickerDateType:dateType dateAble:(YHPickerDateSelectAble_all) showResetToday:NO originDate:originDate title:nil showDateCN:NO format:format didiChangePicker:hintBlock finish:finish config:config];
}
/// 选择时间
/// @param dateType 时间类型
/// @param ableType 未来 过去时间是否可选
/// @param showToday 显示重置今天的按钮
/// @param originDate 初始时间
/// @param title 标题
/// @param hintBlock 有设置就会显示顶部的提示标题 滑动选择变化之后 顶部的标题更新
+ (void)showPickerDateType:(YHPickerDateType)dateType
                  dateAble:(YHPickerDateSelectAble)ableType
            showResetToday:(BOOL)showToday
                originDate:(NSDate * _Nullable)originDate
                     title:(NSString * _Nullable)title
                showDateCN:(BOOL)showDateCN
                    format:(NSString * _Nullable)format
          didiChangePicker:(nullable NSString * _Nonnull (^)(NSString * _Nonnull))hintBlock
                    finish:(void (^ _Nullable)(NSDate * _Nonnull))finish
                    config:(void (^ _Nullable)(YHPickerConfig * _Nonnull))config{
    
    YHPickerViewController * pickerVC = [[YHPickerViewController alloc] init];
    pickerVC.config.pickerHeadTitle = title;
    pickerVC.config.pickerMode = dateType;
    pickerVC.config.picker_able = ableType;
    pickerVC.config.showTodayPicker = showToday;
    pickerVC.config.dateOrigin = originDate;
    pickerVC.config.showDateCN = showDateCN;
    pickerVC.config.dateFormat = format;
    [pickerVC.config setPickerFinishBlock:^(NSString * dateString) {
        NSDate * newDate = [dateString yh_dateByFormate:nil];
        if(finish){
            finish(newDate);
        }
    }];
    [pickerVC.config setPickerDidChangeBlock:^NSString *(NSString *passTitle) {
        if(hintBlock){
            return hintBlock(passTitle);
        }
        return @"";
    }];
    if(config){
        config(pickerVC.config);
    }
    [pickerVC popShow];
}

/// 选择一个字符串数组 或者 人数之类
/// @param dataList 数据
/// @param hintBlock 有设置就会显示顶部的提示标题 滑动选择变化之后 顶部的标题更新
+ (void)showPickerStringList:(NSArray<NSString *> *)dataList
                       title:(NSString *)title
                originSelect:(NSString * _Nullable)selecttitle
            didiChangePicker:(NSString *(^)(NSString *))hintBlock
                      finish:(void (^)(NSString *))finish{
    [YHPickerViewController showPickerStringList:dataList title:title originSelect:selecttitle didiChangePicker:hintBlock finish:finish config:nil];
}
+ (void)showPickerStringList:(NSArray<NSString *> *)dataList
                       title:(NSString *)title
                originSelect:(NSString * _Nullable)selecttitle
            didiChangePicker:(NSString *(^)(NSString *))hintBlock
                      finish:(void (^)(NSString *))finish
                      config:(void (^ _Nullable)(YHPickerConfig * _Nonnull))config{
    
    YHPickerViewController * pickerVC = [[YHPickerViewController alloc] init];
    pickerVC.config.pickerMode = YHPickerDateType_Custom;
    pickerVC.config.pickerHeadTitle = title;
    
    YHPickerItem * pickerItem = [YHPickerItem new];
    pickerItem.pickerDataList = dataList;
    pickerItem.pickerSelectTitle = selecttitle;
    
    pickerVC.config.dataList = @[pickerItem];
    
    [pickerVC.config setPickerFinishBlock:^(NSArray * selectItems) {
        YHPickerItem * item = selectItems.firstObject;
        if(finish){
            finish(item.pickerSelectTitle);
        }
    }];
    [pickerVC.config setPickerDidChangeBlock:^NSString *(NSString *passTitle) {
        if(hintBlock){
            return hintBlock(passTitle);
        }
        return @"";
    }];
    if(config){
        config(pickerVC.config);
    }
    [pickerVC popShow];
}


@end






